package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.ResponseState;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ControllerAdvice
@ResponseBody
public class GlobalController {
    @Autowired
    private Logger logger;

    /**
     * 处理请求参数格式错误的返回信息,form-data格式时
     *
     * @param e：BindException异常
     * @return 111
     */
    @ExceptionHandler
    public ResponseData<Object> BindExceptionHandler(BindException e) {
        System.out.println(e);
        BindingResult bindingResult = e.getBindingResult();
        String message = bindingResult.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return new ResponseData<Object>(message, ResponseState.PARAM_IS_ERROR.getValue());
    }


    /**
     * 处理请求参数格式错误的返回信息，json格式时
     *
     * @param e：MethodArgumentNotValidException异常
     * @return 111
     */
    @ExceptionHandler
    public ResponseData<Object> MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        System.out.println(e);
        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        String message = null;
        for (ObjectError error : errors) {
            if (message != null) {
                message = message + ";" + error.getDefaultMessage();
            } else {
                message = error.getDefaultMessage();
            }
        }
        logger.error(message);
        return new ResponseData<>(message, ResponseState.PARAM_IS_ERROR.getValue());
    }

    /**
     * 处理请求参数格式错误的返回信息，param格式时
     *
     * @param e：MethodArgumentNotValidException异常
     * @return 111
     */
    @ExceptionHandler
    public ResponseData<Object> constraintViolationException(ConstraintViolationException e) {
        String message = e.getMessage();
        logger.error(message);
        return new ResponseData<Object>(message, ResponseState.PARAM_IS_ERROR.getValue());
    }

    /**
     * 请求参数错误
     *
     * @param e：请求参数错误
     * @return 111
     */
    @ExceptionHandler
    public ResponseData<Object> HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error(ResponseState.PARAM_IS_ERROR.getMessage());
        logger.error(e.getLocalizedMessage());
        return new ResponseData<>(ResponseState.PARAM_IS_ERROR.getMessage(), ResponseState.PARAM_IS_ERROR.getValue());
    }


    @ModelAttribute
    public void getModel(Map<String, Object> data, HttpServletRequest request) {
        Family family = (Family) request.getAttribute("familyUser");
        data.put("familyUser", family);
        Volunteer volunteer = (Volunteer) request.getAttribute("volunteerUser");
        data.put("volunteerUser", volunteer);
        Admin admin = (Admin) request.getAttribute("adminUser");
        data.put("adminUser", admin);
        Client client = (Client) request.getAttribute("clientUser");
        data.put("clientUser", client);
    }
}
